package org.xtlgen;

import java.awt.geom.Area;

import org.w3c.dom.Element;

/**
 * Abstract base class for all the closed shapes in the XtlGen system.
 * <p>
 * A closed shaped is one whose lines totally enclose an Area (e.g. polygons,
 * ellipses, etc.). The clipped shape can be calculated in the same way for
 * all this Shapes and therefore this abstract class defines {@link #clipAWTShape}
 * for all of them.
 * </p>
 * <p>
 * Subclasses must still provide the other abstract methods in {@link Shape}.
 * </p>
 */
public abstract class ClosedShape extends Shape {
  /**
   * Construct a ClosedShape from the given AWT Shape
   *
   * @param parent  the parent Shape, null allowed
   * @param awtShape the AWT Shape
   */
  protected ClosedShape(Shape parent, java.awt.Shape awtShape) {
    super(parent, awtShape);
  }
  
  /**
   * Construct a ClosedShape.
   *
   * @param parent  the parent Shape, null allowed
   * @param params  parameters for this Shape as read from the config file
   */
  protected ClosedShape(Shape parent, Element params) {
    super(parent, params);
  }

  /**
   * Clip the AWT shape to the given parent bounds.
   * Called during initialization to clip the AWT shape.
   *
   * @param parent parent Shape
   * @param shape AWT shape to clip
   *
   * @return The resulting clipped AWT shape or null if completely clipped.
   */
  protected java.awt.Shape clipAWTShape(Shape parent, java.awt.Shape shape) {
    if (parent.getAWTShape() != null) {
      Area parentArea = new Area(parent.getAWTShape());
      Area shapeArea  = new Area(shape);
    
      shapeArea.intersect(parentArea);
      if (shapeArea.isEmpty()) {
        // Not visible
        return null;
      }

      return shapeArea;
    }
    // No clipping
    return shape;
  }
};
    
  
